/**
 * 
 */
package com.oauth.common.server.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;

/**
 * @author asanthan
 * 
 */
public class RapWavesPropertyConfigurer extends PropertyPlaceholderConfigurer {

	private static final String LOCAL_PROPERTIES = "local.properties";

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.core.io.support.PropertiesLoaderSupport#setLocations
	 * (org.springframework.core.io.Resource[])
	 */
	@Override
	public void setLocations(Resource[] locations) {

		// Filter local.properties only in production.
		if (CommonUtils.IS_PRODUCTION) {
			List<Resource> newLocations = getFilteredResources(locations);
			super.setLocations(newLocations.toArray(new Resource[newLocations.size()]));
		} else {
			super.setLocations(locations);
		}

	}

	/**
	 * Remove local.properties from the resources.
	 * 
	 * @param locations
	 * @return
	 */
	private List<Resource> getFilteredResources(Resource[] locations) {
		List<Resource> newLocations = new ArrayList<Resource>();
		for (Resource resource : locations) {
			if (!LOCAL_PROPERTIES.equalsIgnoreCase(resource.getFilename())) {
				newLocations.add(resource);
			}
		}
		return newLocations;
	}
}
